package com.ljkj.cordial.lengthtcpheartbeat;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;

public class AlarmManagerUtils {
    private final String ACTION = "com.loonggg.alarm.clock";//接收alarm动作广播
    private final long TIME_INTERVAL = 60 * 1000;//闹钟执行任务的时间间隔
    public AlarmManager am;
    public PendingIntent pendingIntent;
    private volatile boolean isStopAlarm;

    public boolean isStopAlarm() {
        return isStopAlarm;
    }

    public AlarmManagerUtils setStopAlarm(boolean stopAlarm) {
        this.isStopAlarm = stopAlarm;
        return this;
    }


    //
    private AlarmManagerUtils() {
    }

    //饿汉式单例设计模式
    private static AlarmManagerUtils instance = null;

    public static AlarmManagerUtils getInstance() {
        if (instance == null) {
            synchronized (AlarmManagerUtils.class) {
                if (instance == null) {
                    instance = new AlarmManagerUtils();
                }
            }
        }
        return instance;
    }


    public void createGetUpAlarmManager(Context context) {
        am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ACTION);
//        intent.putExtra("msg", "update alarm");
        pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    }

    /**
     * 接收alarm动作广播
     *
     * @param receiver
     */
    public void register(Context context, BroadcastReceiver receiver) {
        createGetUpAlarmManager(context);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION);
        context.registerReceiver(receiver, intentFilter);
    }

    /**
     * 注销alarm动作广播
     */
    public void unRegister(Context context, BroadcastReceiver receiver) {
        context.unregisterReceiver(receiver);
        cancelAlarm();
    }

    @SuppressLint("NewApi")
    public void getUpAlarmManagerStartWork(long interval) {
        //版本适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis(), pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
            am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                    pendingIntent);
        } else {
            am.setRepeating(AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis(), interval == 0L ? TIME_INTERVAL : interval, pendingIntent);
        }
    }

    public void getUpAlarmManagerWorkOnReceiver() {
        if (isStopAlarm()) {
            return;
        }
        getUpAlarmManagerWorkOnReceiver(TIME_INTERVAL);
    }

    @SuppressLint("NewApi")
    public void getUpAlarmManagerWorkOnReceiver(long interval) {
        //高版本重复设置闹钟达到低版本中setRepeating相同效果
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis() + (interval == 0L ? TIME_INTERVAL : interval), pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
            am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                    + (interval == 0L ? TIME_INTERVAL : interval), pendingIntent);
        }
    }

    /**
     * 取消在执行的定时任务
     */
    public void cancelAlarm() {
        try {
//            am.cancel(pendingIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
